package com.gzhryc.shared_device.oem.h5.mng.jetty;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gzhryc.shared_device.oem.code.ResourceAPIServer;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import com.gzhryc.common.PatternHelper;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.system.ManagerService;
import com.gzhryc.system.MenuService;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;
import com.gzhryc.system.dto.MenuData;
import org.apache.commons.lang3.StringUtils;

public class HtmlSecurityFilter implements Filter {

	PatternHelper patternHelper;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		patternHelper = new PatternHelper();
		patternHelper.addExcludePattern(Pattern.compile("/mng/login\\.html"));
		patternHelper.addExcludePattern(Pattern.compile("/mng/about\\.html"));
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = HttpServletRequest.class.cast(request);
		HttpServletResponse httpResponse = HttpServletResponse.class.cast(response);
		
		//检查是否使用本地资源
		boolean localRes = PropertyTools.get().getBoolean("localRes",false);
        if(localRes) {
        	String resDomain = httpRequest.getContextPath();
        	httpRequest.setAttribute("resDomain", resDomain);
        	httpRequest.setAttribute("assDomain", PropertyTools.get().getString("assDomain",resDomain));
        }else {
			String resDomain = PropertyTools.get().getString(WebConstants.CONF_RES_DOMAIN_KEY);
			httpRequest.setAttribute("resDomain", resDomain);
			httpRequest.setAttribute("resToken", ResourceAPIServer.self().buildTransferToken());
			httpRequest.setAttribute("assDomain", PropertyTools.get().getString("assDomain",resDomain));
        }
		
		String uri = httpRequest.getRequestURI();
		if (patternHelper.isExclude(uri)) {
			chain.doFilter(httpRequest, httpResponse);
			return;
		}

		Manager manager = null;
		HttpSession session = httpRequest.getSession(false);
		if (session != null) {
			manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
		}
			
		if(manager == null) {
			//检查COOKIES
			String token = WebUtils.getCookies(httpRequest, SysConstants.COOKIES_LOGIN_TOKEN);
			if(StringTools.isNotBlank(token)) {
				manager = ManagerService.self().getByToken(token);
				if(manager != null) {
					session = httpRequest.getSession(true);
					session.setAttribute(SysConstants.SESSION_LOGIN_MANAGER, manager);
				}
			}
		}
			
		if (manager != null) {
			httpRequest.setAttribute(SysConstants.LOGIN_MANAGER, manager);
			// 菜单
			Object menuDataObj = session.getAttribute(SysConstants.SESSION_LOGIN_MENUS);
			if (menuDataObj != null) {
				httpRequest.setAttribute(SysConstants.MENU_DATA, menuDataObj);
			} else {
				List<MenuData> menuData = MenuService.self().findMenuDataByManagerId(manager.getId());
				session.setAttribute(SysConstants.SESSION_LOGIN_MENUS, menuData);
				httpRequest.setAttribute(SysConstants.MENU_DATA, menuData);
			}
			// 记录日志
			WebConstants.setSysLog(httpRequest, manager);
			httpRequest.setAttribute("uri", StringUtils.removeStart(httpRequest.getRequestURI(), httpRequest.getContextPath()));
			chain.doFilter(httpRequest, httpResponse);
			return;
		}
		
		httpResponse.sendRedirect(httpRequest.getContextPath() + "/login.html");
	}

	@Override
	public void destroy() {
	}
}
